package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketSearchHistoryExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.vx.vo.WxSearchHistoryVo;
import com.cskaoyan.model.vx.vo.WxSearchIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 19:25
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public WxSearchIndexVo indexShow(HttpSession httpSession) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andIsDefaultEqualTo(true);
        List<MarketKeyword> marketKeyword = marketKeywordMapper.selectByExample(marketKeywordExample);

        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        wxSearchIndexVo.setDefaultKeyword(marketKeyword.get(0));

        marketKeywordExample.createCriteria().andIsHotEqualTo(true);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        wxSearchIndexVo.setHotKeywordList(marketKeywords);

        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause("add_time desc");
        String wxUserId = httpSession.getAttribute("wxUserId").toString();
        if (wxUserId==null){
            marketSearchHistoryExample.createCriteria().andUserIdEqualTo(0).andDeletedEqualTo(false);
        }else{
            marketSearchHistoryExample.createCriteria().andUserIdEqualTo(Integer.parseInt(wxUserId)).andDeletedEqualTo(false);
        }
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        List<WxSearchHistoryVo> historyVos = new ArrayList<>();
        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            WxSearchHistoryVo wxSearchHistoryVo = new WxSearchHistoryVo();
            wxSearchHistoryVo.setKeyword(marketSearchHistory.getKeyword());
            historyVos.add(wxSearchHistoryVo);
        }
        wxSearchIndexVo.setHistoryKeywordList(historyVos);
        return wxSearchIndexVo;
    }

    @Override
    public List<String> searchHelp(String keyword) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        ArrayList<String> strings = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            strings.add(marketKeyword.getKeyword());
        }
        return strings;
    }

    @Override
    public void searchClearHistory(HttpSession httpSession) {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.createCriteria().andUserIdEqualTo((Integer) httpSession.getAttribute("wxUserId"));
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            MarketSearchHistory marketSearchHistory1 = new MarketSearchHistory();
            marketSearchHistory1.setUpdateTime(new Date());
            marketSearchHistory1.setDeleted(true);
            MarketSearchHistoryExample marketSearchHistoryExample1 = new MarketSearchHistoryExample();
            marketSearchHistoryExample1.createCriteria().andIdEqualTo(marketSearchHistory.getId());
            marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory1, marketSearchHistoryExample1);
        }
    }
}
